//
//  OVNavViewController.m
//  OverseasLiveing
//
//  Created by ZZF on 14-10-10.
//  Copyright (c) 2014年 张洲峰. All rights reserved.
//

#import "OVNavViewController.h"
#import "CustomBarItem.h"
#import "SVGloble.h"
#import "UINavigationItem+CustomItem.h"
@interface OVNavViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong)UIViewController *controller;

@end

@implementation OVNavViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    self.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"tab_bg"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[SVGloble colorFromHexRGB:@"fbc224"],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:20],NSFontAttributeName, nil]];
    if (iOS7) {
        self.interactivePopGestureRecognizer.delegate = nil;
    }

//        UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
//        gesture.enabled = NO;
//        UIView *gestureView = gesture.view;
//        
//        UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] init];
//        popRecognizer.delegate = self;
//        popRecognizer.maximumNumberOfTouches = 1;
//        [gestureView addGestureRecognizer:popRecognizer];
//        /**
//         *  获取系统手势的target数组
//         */
//        NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
//        /**
//         *  获取它的唯一对象，我们知道它是一个叫UIGestureRecognizerTarget的私有类，它有一个属性叫_target
//         */
//        id gestureRecognizerTarget = [_targets firstObject];
//        /**
//         *  获取_target:_UINavigationInteractiveTransition，它有一个方法叫handleNavigationTransition:
//         */
//        id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];
//        /**
//         *  通过前面的打印，我们从控制台获取出来它的方法签名。
//         */
//        SEL handleTransition = NSSelectorFromString(@"handleNavigationTransition:");
//        /**
//         *  创建一个与系统一模一样的手势，我们只把它的类改为UIPanGestureRecognizer
//         */
//        [popRecognizer addTarget:navigationInteractiveTransition action:handleTransition];
}

- (void)backButtonClicked:(UIBarButtonItem *)item
{
    [self.controller.navigationController popViewControllerAnimated:YES];

}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    [super popViewControllerAnimated:animated];
    self.controller = [self.viewControllers lastObject];
    return self.controller;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        // 加上back按钮
        self.controller = viewController;
        CustomBarItem *leftItem = [viewController.navigationItem setItemWithImage:@"back_1" size:CGSizeMake(52, 44) itemType:left];
        [leftItem setOffset:-10];//设置item偏移量(正值向右偏，负值向左偏)
        [leftItem addTarget:self selector:@selector(backButtonClicked:) event:(UIControlEventTouchUpInside)];
        self.controller.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)hideBackBarItem:(BOOL)hide
{
    if (hide) {
        self.controller.navigationItem.hidesBackButton = YES;
        self.controller.navigationItem.leftBarButtonItem = nil;
    } else {
        self.controller.navigationItem.hidesBackButton = YES;
        self.controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
    }
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    /**
     *  这里有两个条件不允许手势执行，1、当前控制器为根控制器；2、如果这个push、pop动画正在执行（私有属性）
     */
    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
}
@end
